#ifndef FENGGO_AI_UTIL
#define FENGGO_AI_UTIL

#include "FengGo.hpp"

namespace magicube{

extern long nowBoard[] ;
class util{

public:
	util(void){}
	~util(void){}

public:
	
	//棋手落子
	int sitting(char *sCols, char *sLines){
		int  iLine;
		char cCol;
		
		iLine = atoi(sLines) - 1;
		cCol   = sCols[0];

		//判断输入的正确性
		if(!(strlwc(cCol) >= 'a' && strlwc(cCol) <= 's')){
			return 9;
		}
		if((iLine > 18) || (iLine < 0)){
			return 9;
		}
		
		//落子操作
		switch(cCol){
			case 'a':
			case 'A':
				if(((nowBoard[LINE_A] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_A] = nowBoard[LINE_A] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 'b':
			case 'B':
				if(((nowBoard[LINE_B] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_B] = nowBoard[LINE_B] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 'c':
			case 'C':
				if(((nowBoard[LINE_C] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_C] = nowBoard[LINE_C] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 'd':
			case 'D':
				if(((nowBoard[LINE_D] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_D] = nowBoard[LINE_D] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 'e':
			case 'E':
				
				if(((nowBoard[LINE_E] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_E] = nowBoard[LINE_E] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 'f':
			case 'F':
				
				if(((nowBoard[LINE_F] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_F] = nowBoard[LINE_F] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 'g':
			case 'G':
				
				if(((nowBoard[LINE_G] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_G] = nowBoard[LINE_G] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 'h':
			case 'H':
				
				if(((nowBoard[LINE_H] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_H] = nowBoard[LINE_H] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 'i':
			case 'I':
				if(((nowBoard[LINE_I] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_I] = nowBoard[LINE_I] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 'j':
			case 'J':
				if(((nowBoard[LINE_J] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_J] = nowBoard[LINE_J] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 'k':
			case 'K':
				if(((nowBoard[LINE_K] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_K] = nowBoard[LINE_K] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 'l':
			case 'L':
				if(((nowBoard[LINE_L] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_L] = nowBoard[LINE_L] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 'm':
			case 'M':
				if(((nowBoard[LINE_M] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_M] = nowBoard[LINE_M] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 'n':
			case 'N':
				if(((nowBoard[LINE_N] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_N] = nowBoard[LINE_N] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 'o':
			case 'O':
				if(((nowBoard[LINE_O] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_O] = nowBoard[LINE_O] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 'p':
			case 'P':
				if(((nowBoard[LINE_P] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_P] = nowBoard[LINE_P] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 'q':
			case 'Q':
				if(((nowBoard[LINE_Q] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_Q] = nowBoard[LINE_Q] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 'r':
			case 'R':
				if(((nowBoard[LINE_R] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_R] = nowBoard[LINE_R] | ((long)0x0000000000000001 << (iLine * 2));
				}
				break;
			case 's':
			case 'S':
				if(((nowBoard[LINE_S] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_S] = nowBoard[LINE_S] | ((long)0x0000000000000001 << (iLine * 2));
				}break;
		}
		
		return 0;
	}
	//FengGo落子
	int AIsitting(char *sCols, char *sLines){
		int  iLine;
		char cCol;
		
		iLine = atoi(sLines) - 1;
		cCol   = sCols[0];
		
		//判断输入的正确性
		if(!(strlwc(cCol) >= 'a' && strlwc(cCol) <= 's')){
			return 9;
		}
		if((iLine > 18) || (iLine < 0)){
			return 9;
		}
		
		//落子操作
		switch(cCol){
			case 'a':
			case 'A':
				if(((nowBoard[LINE_A] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_A] = nowBoard[LINE_A] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 'b':
			case 'B':
				if(((nowBoard[LINE_B] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_B] = nowBoard[LINE_B] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 'c':
			case 'C':
				if(((nowBoard[LINE_C] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_C] = nowBoard[LINE_C] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 'd':
			case 'D':
				if(((nowBoard[LINE_D] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_D] = nowBoard[LINE_D] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 'e':
			case 'E':
				
				if(((nowBoard[LINE_E] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_E] = nowBoard[LINE_E] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 'f':
			case 'F':
				
				if(((nowBoard[LINE_F] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_F] = nowBoard[LINE_F] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 'g':
			case 'G':
				
				if(((nowBoard[LINE_G] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_G] = nowBoard[LINE_G] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 'h':
			case 'H':
				
				if(((nowBoard[LINE_H] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_H] = nowBoard[LINE_H] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 'i':
			case 'I':
				if(((nowBoard[LINE_I] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_I] = nowBoard[LINE_I] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 'j':
			case 'J':
				if(((nowBoard[LINE_J] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_J] = nowBoard[LINE_J] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 'k':
			case 'K':
				if(((nowBoard[LINE_K] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_K] = nowBoard[LINE_K] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 'l':
			case 'L':
				if(((nowBoard[LINE_L] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_L] = nowBoard[LINE_L] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 'm':
			case 'M':
				if(((nowBoard[LINE_M] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_M] = nowBoard[LINE_M] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 'n':
			case 'N':
				if(((nowBoard[LINE_N] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_N] = nowBoard[LINE_N] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 'o':
			case 'O':
				if(((nowBoard[LINE_O] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_O] = nowBoard[LINE_O] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 'p':
			case 'P':
				if(((nowBoard[LINE_P] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_P] = nowBoard[LINE_P] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 'q':
			case 'Q':
				if(((nowBoard[LINE_Q] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_Q] = nowBoard[LINE_Q] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 'r':
			case 'R':
				if(((nowBoard[LINE_R] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_R] = nowBoard[LINE_R] | ((long)0x0000000000000002 << (iLine * 2));
				}
				break;
			case 's':
			case 'S':
				if(((nowBoard[LINE_S] >> (iLine * 2)) & 0x0000000000000003) != 0){
					AIlog.ErrLog("坐标[%s][%s]已落子\n", sCols, sLines);
					return 2;
				}else{
					nowBoard[LINE_S] = nowBoard[LINE_S] | ((long)0x0000000000000002 << (iLine * 2));
				}break;
		}
		
		return 1;
	}
	
	//将大写字母转为小写
	char strlwc(char c){
		char a;
		
		a = c;
		
		if(a>='A' && a<='Z')
			a+=32;
		
		return a;
	}

	//读取配置文件信息
	int getINI(char * pTitle, char * pFlag, char * pResult){
		char sFileName[255 + 1];
		char sBuf[255 + 1];
		char sBufTmp[255 + 1];
		int  iInTitleFlag = 0;//进入标题标志 0-未进入  1-进入
		
		memset(sFileName, 0x00, sizeof(sFileName));
		sprintf(sFileName, "%s%s", getenv("HOME"), INI_FILE);
		
		std::fstream infile(sFileName, std::fstream::in);
		if(!infile) {
			AIlog.ErrLog("读ini错误");
		}
		while(1){
			memset(sBuf, 0x00, sizeof(sBuf));
			infile.getline(sBuf,255);
			
			//跳过以#开头的行
			if(sBuf[0] == '#')
				continue;
			
			if((strstr(sBuf, pTitle) != NULL) && (iInTitleFlag == 0)){
				iInTitleFlag = 1;
			}
			
			if((iInTitleFlag == 1) && (strstr(sBuf, pFlag) != NULL)){
				memset(sBufTmp, 0x00, sizeof(sBufTmp));
				strcpy(sBufTmp, sBuf + strlen(pFlag));
				utilstrcut(sBufTmp, '#', 255);  //sizeof(pResult));
				allTrim(sBufTmp);
				break;
			}
		}
		
		strcpy(pResult, sBufTmp);
		//infile.clear();
		//infile.seekg(0);
		//while(infile>>i) cout<<i<<'?';

		infile.close();
		
		return 0;
	}
	
	int utilstrcut(char * sWord, char cCut, int iLong){
		int i;
		
		for(i = 0; i < iLong; i++){
			if(sWord[i] == cCut){
				sWord[i] = 0;
				break;
			}
		}
		
		if(i == iLong)
			sWord[iLong - 1] = 0;
		
		return 0;
	}
	
	int allTrim(char * sWord){
		char sBuf[255 + 1];
		int i, iLong;
		
		//去掉左空格
		iLong = strlen(sWord);
		memset(sBuf, 0x00, sizeof(sBuf));
		
		for (i = 0; i < iLong; i++){
			if (sWord[i] != ' '){
				strcpy(sBuf, sWord);
				break;
			}
		}
		
		//去掉右空格
		iLong = strlen(sBuf);
		
		for (i = 0; i < iLong; i++){
			if (sBuf[i] == ' '){
				sBuf[i] = 0;
				break;
			}
		}
		
		strcpy(sWord, sBuf);
		
		return 0;
	}
};

util AIutil;
}
#endif
